<?php

/**
 * MLEPP - ManiaLive Extending Plugin Pack
 *
 * -- MLEPP Plugin --
 * @name Custom Chat
 * @date 04-01-2011
 * @version r934
 * @website mlepp.trackmania.nl
 * @package MLEPP
 *
 * @author The MLEPP team
 * @copyright 2010 - 2011
 *
 * ---------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 * You are allowed to change things or use this in other projects, as
 * long as you leave the information at the top (name, date, version,
 * website, package, author, copyright) and publish the code under
 * the GNU General Public License version 3.
 * ---------------------------------------------------------------------
 */

namespace ManiaLivePlugins\MLEPP\CustomChat;

use ManiaLive\Utilities\Console;
use ManiaLive\DedicatedApi\Connection;
use ManiaLive\Data\Storage;
use ManiaLive\Config\Loader;

use ManiaLivePlugins\MLEPP\Core\Core;
use ManiaLivePlugins\MLEPP\Core\Mlepp;
use ManiaLive\Features\Admin\AdminGroup;

class CustomChat extends \ManiaLive\PluginHandler\Plugin {
	private $mlepp;

	 /**
     * onInit()
     * Function called on initialisation of ManiaLive.
     *
     * @return void
     */

	function onInit() {
		// this needs to be set in the init section
		$this->setVersion(934);
        $this->setPublicMethod('getVersion');
        //ML Repository
        if(Core::RepositoryId != null){
            $this->setRepositoryId(Core::RepositoryId);
            $this->setRepositoryVersion(Core::RepositoryVersion);
        }
	}

    /**
     * onLoad()
     * Function called on loading of ManiaLive.
     *
     * @return void
     */

	function onLoad() {
		$this->enableDedicatedEvents();
		Console::println('['.date('H:i:s').'] [MLEPP] Plugin: CustomChat r'.$this->getVersion() );

		try {
			$this->connection->chatEnableManualRouting(true);
		}
		catch (\Exception $e) {
				Console::println('['.date('H:i:s').'] [MLEPP] [CustomChat] Couldn\'t initialize custom chat.'."\n".' Error from server: '.$e->getMessage());
				die();
		}

		$this->mlepp = Mlepp::getInstance();
	}

    /**
     * onPlayerChat()
     * Function called when someone is chatting.
     *
     * @param mixed $playerUid
     * @param mixed $login
     * @param mixed $chat
     * @param mixed $isRegistredCmd
     * @return
     */

	function onPlayerChat($playerUid,$login,$text,$isRegistredCmd) {
		if($playerUid != 0) {
			$source_player = $this->storage->getPlayerObject($login);
			$nick = $source_player->nickName;
            $nick = str_replace('$w','',$nick);
			$text = str_replace('$l','',$text);
			//$text = str_replace('$h','',$text);
			$text = preg_replace('/(^|[ ]|((https?|ftp):\/\/))(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|net|org|info|biz|gov|name|edu|[a-zA-Z][a-zA-Z]))(:[0-9]+)?((\/|\?)[^ "]*[^ ,;\.:">)])?/i','\$l$0\$l',$text);
			//$text = preg_replace('/(^|[ ]|(tmtp):\/\/|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|net|org|info|biz|gov|name|edu|[a-zA-Z][a-zA-Z]))(:[0-9]+)?((\/|\?)[^ "]*[^ ,;\.:">)])?/i','\$h$0\$h',$text);
			if(substr($text,0,1) != "/") {
				try {
					if(in_array($login,$this->mlepp->AdminGroup->getAdminsByGroup('root'))) {
					   $this->connection->chatSendServerMessage("\$fff".Core::$rootPrefix."$nick\$z\$s".Core::$adminChatColor."  ".$text);
					}
                    elseif( in_array($login,$this->mlepp->AdminGroup->getAdminsByGroup('admin'))) {
						$this->connection->chatSendServerMessage("\$fff".Core::$adminPrefix."$nick\$z\$s".Core::$adminChatColor."  ".$text);
					}
					elseif (in_array($login,$this->mlepp->AdminGroup->getAdminsByGroup('operator')) ) {
							$this->connection->chatSendServerMessage("\$fff".Core::$operatorPrefix."$nick\$z\$s".Core::$adminChatColor."  ".$text);
					}
						else {
						$this->connection->chatSendServerMessage("\$fff$nick\$z\$s".Core::$chatColor."  ".$text);
					}
				}
				catch (\Exception $e) {
					Console::println('['.date('H:i:s').'] [MLEPP] [CustomChat] error sendin chat from.'.$login.': '.$e->getMessage());
					$this->connection->chatSendServerMessage("error sending chat:".$e->getMessage(),$source_player);
				}
			}
		}
	}

	/**
	 * onUnload()
     * Function called on unloading this plugin.
	 *
	 * @return void
	 */

	function onUnload() {
		$this->connection->chatEnableManualRouting(false);
		parent::onUnload();
	}

}
?>